package com.heima.model;

import com.heima.domain.User;
import com.heima.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 处理数据的javabean
 */

public class UserModel {

    //处理用户登录的方法
    //连接数据库，通过传入的用户名和密码去数据库中进行查询

    public int login(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        String userName = user.getUserName();
        String password = user.getPassword();
        int result = 0;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();

            String sql = "select count(*) from users where userName=? and password=?";

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, userName);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                result = rs.getInt("count(*)");

            }
            result = pstmt.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(pstmt, conn);
        }

        return result;
    }
}
